/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb.metier;

import com.ejb.persistence.Address;
import com.ejb.persistence.User;
import com.ejb.persistence.UserAuthentication;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Wanted
 * Class used to manage User
 * 
 */
@Stateless
public class UserManagerBean implements IUserManager {

    @PersistenceContext
    EntityManager em;
   
    /**
     * 
     * Return the user if the authentification succed
     * @param login 
     * @param password
     * @return User
     * 
     */
    @Override
   public User validateConn(String login, String password) {
      
      List<User> retour = null;
      UserAuthentication uAuth = new UserAuthentication();
      
        try {
            password = Encryption.cryptPassword(password);
            uAuth.setLogin(login);
            uAuth.setPassword(password);
            Query q = em.createNamedQuery("User.exists");
            q.setParameter("userAuth", uAuth);
            retour = q.getResultList();
            System.out.println(retour.size());
            System.out.println(retour.get(0).getUserAuth().getLogin());
            if(retour.size() == 1)
            {
                return retour.get(0);
            }
            else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }

   }
   
    /**
     * 
     * Create a new user
     * @param user 
     * @param address
     * @return boolean
     * 
     */
    @Override
    public boolean createAccount(User user, Address address) {
       List<User> retour = null;
       try {

            
            user.getUserAuth().setPassword(Encryption.cryptPassword(user.getUserAuth().getPassword()));
            Query q = em.createNamedQuery("User.inscription");
            q.setParameter("userlog", user.getUserAuth().getLogin() );
            retour = q.getResultList();
            if(retour.size()==0)
            {
                em.persist(user);
                em.persist(address);
                return true;
            }
            else{
                return false;
            }

           
       } catch (Exception e) {
           return false;
       }
   }
    
    /**
     * 
     * Update the account of one user
     * @param login 
     * @param password
     * @return boolean
     * 
     */
    @Override
    public boolean updateAccount(User user, Address address){
        try {           
            // Crypt password if his lenght is not 40 chars.
            user.getUserAuth().setPassword((user.getUserAuth().getPassword().length() != 40)? Encryption.cryptPassword(user.getUserAuth().getPassword()) : user.getUserAuth().getPassword() );
            em.merge(user);
            em.merge(address);
            return true;
           
       } catch (Exception e) {
           return false;
       }
    }
   
    /**
     * 
     * Return the user information
     * @param idClient 
     * @return User
     * 
     */
    @Override
   public User getClientInfo(long idClient) {
      User u;
        try {
           u = em.find(User.class, idClient);
           return u;
        } catch (Exception e) {
            return null;
        }
   }
}
